package com.example.oj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oj.entity.QuestionAnswer;
import com.example.oj.entity.dto.questionAnswer.AnswerSaveRequest;
import com.example.oj.entity.vo.QuestionAnswerVO;
import com.example.oj.mapper.QuestionAnswerMapper;
import com.example.oj.service.QuestionAnswerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 28651
 * @description 针对表【questionAnswer】的数据库操作Service实现
 * @createDate 2024-04-19 18:19:39
 */
@Service
public class QuestionAnswerServiceImpl extends ServiceImpl<QuestionAnswerMapper, QuestionAnswer>
    implements QuestionAnswerService {

  @Resource
  private QuestionAnswerMapper mapper;

  @Override
  public QuestionAnswer getByQuestionId(Long questionId) {
    QueryWrapper<QuestionAnswer> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(questionId != null, "questionId", questionId);
    return mapper.selectOne(queryWrapper);
  }

  @Override
  public QuestionAnswerVO getAnswerVO(QuestionAnswer questionAnswer) {
    QuestionAnswerVO answerVO = new QuestionAnswerVO();
    answerVO.setId(questionAnswer.getId());
    answerVO.setQuestionId(questionAnswer.getQuestionId());
    answerVO.setTitle(questionAnswer.getTitle());
    answerVO.setContent(questionAnswer.getContent());
    answerVO.setCode(questionAnswer.getCode());
    answerVO.setCreateTime(questionAnswer.getCreateTime());
    return answerVO;
  }

  @Override
  public Integer addAnswer(AnswerSaveRequest saveRequest) {
    QuestionAnswer questionAnswer = new QuestionAnswer();
    questionAnswer.setId(saveRequest.getId());
    questionAnswer.setQuestionId(saveRequest.getQuestionId());
    questionAnswer.setTitle(saveRequest.getTitle());
    questionAnswer.setContent(saveRequest.getContent());
    questionAnswer.setCode(saveRequest.getCode());
    if (questionAnswer.getId() != null) {
      return mapper.updateById(questionAnswer);
    } else {
      return mapper.insert(questionAnswer);
    }
  }

  @Override
  public Integer getHasAnswer(Long questionId) {
    QueryWrapper<QuestionAnswer> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(questionId != null, "questionId", questionId);
    QuestionAnswer questionAnswer = mapper.selectOne(queryWrapper);
    return questionAnswer == null ? 0 : 1;
  }
}




